package com.theexceptions.todo.controller;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;
import com.theexceptions.todo.model.DatabaseHelper;
import com.theexceptions.todo.model.IPersister;
import com.theexceptions.todo.model.Location;
import com.theexceptions.todo.model.Task;
import com.theexceptions.todo.view.LocationItemizedOverlay;
import com.theexceptions.todo.view.R;

/**
 * Controller that manages the functionality of the view for viewing a user's
 * current tasks on the map.
 * 
 * @author Justian Meyer
 */
public class ShowMapController extends Controller {

	/**
	 * Returns an overlay consisting of all locations associated with all of the
	 * current user's tasks.
	 * 
	 * @param context Activity context
	 * @return LocationItemizedOverlay
	 */
	public static LocationItemizedOverlay getCurrentTasksLocationItemizedOverlay(Activity activity) {
		IPersister dh = new DatabaseHelper(activity);
		List<Task> tasks = dh.retrieveTasks(currentUser.getUserID());

		if (tasks.size() <= 0)
			return null;

		Drawable drawable = activity.getResources().getDrawable(R.drawable.marker);
		LocationItemizedOverlay itemizedOverlay = new LocationItemizedOverlay(drawable, activity);

		for (Task task : tasks) {
			Location loc = task.getLocation();

			int lat = loc.getLatitude();
			int lon = loc.getLongitude();

			GeoPoint point = new GeoPoint(lat, lon);
			OverlayItem overlayitem = new OverlayItem(point, task.getTaskName(), "Location: " + loc.getName());

			itemizedOverlay.addOverlay(overlayitem);
		}

		return itemizedOverlay;
	}

}
